//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "WekaLoadAttributeSelectionBuffer.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE  10000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
WekaLoadAttributeSelectionBuffer::WekaLoadAttributeSelectionBuffer () {
  
  this->namefileBuffer  = new String ();

  this->description     = new String ();
  this->listAttributes  = new TListE <String> ();

  this->toString        = new String ();
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
WekaLoadAttributeSelectionBuffer::WekaLoadAttributeSelectionBuffer (const WekaLoadAttributeSelectionBuffer& wekaLoadAttributeSelectionBuffer) {
  
  this->namefileBuffer  = new String (wekaLoadAttributeSelectionBuffer.namefileBuffer);

  this->description     = new String (wekaLoadAttributeSelectionBuffer.description);
  this->listAttributes  = new TListE <String> (wekaLoadAttributeSelectionBuffer.listAttributes);

  this->toString        = new String (wekaLoadAttributeSelectionBuffer.toString);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
WekaLoadAttributeSelectionBuffer::WekaLoadAttributeSelectionBuffer (const WekaLoadAttributeSelectionBuffer* wekaLoadAttributeSelectionBuffer) {
  
  this->namefileBuffer  = new String (wekaLoadAttributeSelectionBuffer->namefileBuffer);

  this->description     = new String (wekaLoadAttributeSelectionBuffer->description);
  this->listAttributes  = new TListE <String> (wekaLoadAttributeSelectionBuffer->listAttributes);

  this->toString        = new String (wekaLoadAttributeSelectionBuffer->toString);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
WekaLoadAttributeSelectionBuffer::~WekaLoadAttributeSelectionBuffer () {
  
  if (this->namefileBuffer) delete this->namefileBuffer;
  if (this->description)    delete this->description;
  if (this->listAttributes) delete this->listAttributes;
  if (this->toString)       delete this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void WekaLoadAttributeSelectionBuffer::NamefileBuffer (String* namefileBuffer) {
  
  if (namefileBuffer)
    this->namefileBuffer->In(namefileBuffer);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void WekaLoadAttributeSelectionBuffer::Description (String* description) {
  
  if (description)
    this->description->In(description);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void WekaLoadAttributeSelectionBuffer::ListAttributes (TListE <String>* listAttributes) {
  
  if (listAttributes)
    *(this->listAttributes) = *listAttributes;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* WekaLoadAttributeSelectionBuffer::NamefileBuffer (void) {
  
  return this->namefileBuffer;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* WekaLoadAttributeSelectionBuffer::Description (void) {
  
  return this->description;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <String>* WekaLoadAttributeSelectionBuffer::ListAttributes (void) {
  
  return this->listAttributes;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* WekaLoadAttributeSelectionBuffer::ToString (void) {
  
  this->toString->In("");
  
  this->listAttributes->SetInitial();
  while (this->listAttributes->SetNext()) {
    this->toString->Add(this->listAttributes->GetCurrent());
    this->toString->Add("\n");
  }
  
  return this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool WekaLoadAttributeSelectionBuffer::LoadData (void) {
  
  if (!this->namefileBuffer || !this->namefileBuffer->Length())
    return false;
  
  fstream file;
  
  file.open(this->namefileBuffer->Out(), iostream::in);
  
  if (!file.is_open()) 
    return false;
  
  this->listAttributes->Clear();
  this->description->In("");
  
  char  buffer [LENGTHLINE];
  
  bool  readDesription  = false;
  bool  readAttributes  = false;
  
  int   numberAttribute = 0;
  
  while (!file.eof()) {
    
    file.getline(buffer, LENGTHLINE);
    
    if (readDesription && !strlen(buffer))
      readDesription = false;

    this->description->Add(buffer);
    
    if (strstr(buffer, "Search Method:"))
      readDesription = true;
    
    if (strstr(buffer, "Selected attributes:")) {
      
      readAttributes = true;
      sscanf(buffer, "Selected attributes: %*s : %d", &numberAttribute);
      
      for (int i = 0; i < numberAttribute; i++) {
        
        if (file.eof()) break;
        
        file.getline(buffer, LENGTHLINE);
        
        String* pattern = new String (buffer);
        pattern->ITrim();
        
        this->listAttributes->Add(pattern);
        
      }
      
    }
    
  }
  
  file.close();
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool WekaLoadAttributeSelectionBuffer::LoadListFromFile (String* namefileList) {
  
  if (!namefileList || !namefileList->Length())
    return false;
  
  fstream file;
  
  file.open(namefileList->Out(), iostream::in);
  
  if (!file.is_open()) 
    return false;
  
  this->listAttributes->Clear();
  this->description->In("");
  
  char  buffer [LENGTHLINE];
  
  while (!file.eof()) {
    
    file.getline(buffer, LENGTHLINE);

    String* pattern = new String (buffer);
    pattern->ITrim();

    this->listAttributes->Add(pattern);

  }
  
  file.close();
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool WekaLoadAttributeSelectionBuffer::MergeList(TListE<String>* listAttributesToMerge) {
  
  if (!listAttributesToMerge)
    return false;
  
  listAttributesToMerge->SetInitial();
  while (listAttributesToMerge->SetNext()) {
    
    bool found = false;
    
    this->listAttributes->SetInitial();
    while (this->listAttributes->SetNext())
      if (!strcmp(this->listAttributes->GetCurrent()->Out(), listAttributesToMerge->GetCurrent()->Out())) {
        found = true;
        break;
      }
    
    if (!found)
      this->listAttributes->Add(*(listAttributesToMerge->GetCurrent()));
    
  }
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool WekaLoadAttributeSelectionBuffer::WriteSummary (String* filenameOut) {
  
  if (!filenameOut || !filenameOut->Length())
    return false;
  
  fstream file;
  
  file.open(filenameOut->Out(), fstream::out);
  
  if (!file.is_open())
    return false;
  
  this->listAttributes->SetInitial();
  while (this->listAttributes->SetNext())
    file << this->listAttributes->GetCurrent()->Out() << endl;
  
  file.close();
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

